<template>
  <div>
    <el-row style="margin: 160px 600px 100px 0px;">
      <el-col :span="12">&nbsp;</el-col>
      <el-col :span="9">
        <h1 style="text-align: center; margin: 20px 0;">用户登录</h1>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="ruleForm.userName" size="small"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" size="small"></el-input>
          </el-form-item>
<!--          <el-form-item label="验证码" prop="kaptcha">-->
<!--            <el-input v-model="ruleForm.kaptcha" size="small"></el-input>-->
<!--&lt;!&ndash;            <img @click="getKaptchaImg" src="http://localhost:8888/kaptcha">&ndash;&gt;-->
<!--            <a href="http://localhost:8080/">-->
<!--              <img src="http://localhost:8888/kaptcha"/></a>-->
<!--          </el-form-item>-->
          <el-form-item style="margin:10px 100px 10px 100px">
            <el-button size="small" type="primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button size="small" @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>

<!--        <div style="text-align: center;">-->
<!--          <el-link href="/admin">直接进入后面管理界面（临时）</el-link>-->
<!--        </div>-->
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Common from "@/views/common/Common"
export default {
  data() {
    return {
      ruleForm: {
        userName: '',
        password: '',
        // kaptcha:''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('111')
          let url = Common.data.BASEURI+'/sso/admin/login';
          console.log('url >>> ' + url);
          let data = {
            'userName': this.ruleForm.userName,
            'password': this.ruleForm.password,
          };
          console.log('data >>> ');
          console.log(data);
          this.axios.post(url, data).then((response) => {
            console.log(response.data);
            if (response.data.code == 200) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });

              // 获取服务器端响应的JWT并存入到LocalStorage中
              let jwt = response.data.data;
              // console.log("jwt=========="+jwt);
              localStorage.setItem('jwt', jwt);
              // 跳转到主页
              this.$router.push('/admin');

            } else if (response.data.code != 200) {
              // this.$message.error('登录失败，用户名不存在！');
              // this.$message.error('登录失败，用户名或密码错误');
              this.$message.error(response.data.message);

            } else {
              // this.$message.error('登录失败，密码错误！');
              this.$message.error(response.data.message);
            }
          })
          //     .catch(function(error) {
          //   console.log('响应结果为失败！');
          // })
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    setTimeout(() => {
      this.$router.push({path: "/admin"});
    }, 60000000);
  }

}
</script>